/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.bookmarkHelper;

import com.googlecode.mindlistening.tray.Messages;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import org.codehaus.jackson.JsonNode;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.ViewQuery;

/**
 *
 * @author ryan
 */
public class BookmarkDropTargetWindow extends JWindow {
    private Dimension windowSizeAsIcon;
    private Dimension windowSizeExpanded;
    private int currentX;
    private int currentY;
    private JPanel display;
    private DropFormWebpage form;
    private CouchDbInstance dbInstance;
    JDialog d ;
    
    public BookmarkDropTargetWindow(int x, int y, List<String> spaces,  CouchDbInstance dbInstance) {
        this.dbInstance = dbInstance;
        windowSizeAsIcon = new Dimension(16, 16);
        windowSizeExpanded = new Dimension(420, 480);
        this.currentX = x;
        this.currentY = y;
        display = new JPanel();
        display.setLayout(new BorderLayout() );
        display.setBackground(Color.gray);
        display.setToolTipText("Drag items from a webpage into here to bookmark");
        getContentPane().add(display);

        JLabel label = new JLabel(new ImageIcon("mind_small.png"));
        label.setSize(16, 16);
        display.add(label, BorderLayout.CENTER);

        setSize(windowSizeAsIcon);
        setLocation(x, y);
        setAlwaysOnTop(true);
        form = new DropFormWebpage();
        form.setBookmarkDropTargetWindow(this);
        form.setDBInstance(dbInstance);
        form.setSpaces(spaces);

        display.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
            }
        });
        display.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                setLocation(getLocation().x + (e.getX() - currentX), getLocation().y + (e.getY() - currentY));
            }
        });

        DropTarget dt = new DropTarget(this, new DropTargetAdapter() {


            @Override
            public void dragEnter(DropTargetDragEvent dtde) {
                display.setBackground(Color.green);
            }

            public void dragExit(DropTargetEvent dte) {
                display.setBackground(Color.gray);
            }


            @Override
            public void drop(DropTargetDropEvent dtde) {
                try {
                    display.setBackground(Color.gray);
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    Transferable t = dtde.getTransferable();
                    // we should be checking what this is...local file, url, etc...
                    final String url = (String) t.getTransferData(DataFlavor.stringFlavor);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            showBookmarkDialog(url);
                        }
                    });
                } catch (UnsupportedFlavorException ex) {
                    Logger.getLogger(BookmarkDropTargetWindow.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(BookmarkDropTargetWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    protected void showBookmarkDialog(String url) {
        if (d == null) {
            d = new JDialog();
            d.setTitle("Bookmark");
            d.setSize(windowSizeExpanded);
            d.setLocationRelativeTo(null);
            d.getContentPane().add(form);
            form.setContainer(d);
        }

        form.setWebsiteAddress(url);
        d.setVisible(true);
        form.getDescriptionField().requestFocus();
    }


    protected void returnToIcon() {

    }


}
